package main.java.com.extract;


import java.util.Enumeration;
import java.util.Vector;

/**
 * @description: 重构后的主体代码
 * @author: cong zhi
 * @createDate: 2022/6/16 7:54
 * @updateUser: cong zhi
 * @updateDate: 2022/6/16 7:54
 * @updateRemark: 修改内容
 * @version: v1.0
 */
public class Customer {

    private String name;

    private Vector<Rental> _rentals = new Vector<Rental>();

    public Customer(String name) {
        this.name = name;
    }

    public void addRental(Rental arg) {
        _rentals.addElement(arg);
    }

    public String getName() {
        return name;
    }

    public Vector<Rental> getRentals() {
        return _rentals;
    }

    public String textStatement() {
        Enumeration rental = _rentals.elements();
        String result = "Rental Record for " + getName();
        while (rental.hasMoreElements()) {
            Rental each = (Rental) rental.nextElement();
            result += "\n\t   " + each.getMovie().getTitle() + "\t" + each.getAmount();
        }
        result += "\n Amount owed is " + getTotalAmount();
        result += "\n You earned " + getTotalfrequentRenterPoints() + " frequent renter points";
        return result;
    }

    public String htmlStatement() {
        Enumeration rental = _rentals.elements();
        String result = "<H1>Rental Record for <EM>" + getName() + "</EM></H1>\n\t";
        while (rental.hasMoreElements()) {
            Rental each = (Rental) rental.nextElement();
            result += each.getMovie().getTitle() + ":" + each.getAmount() + " <BR>\n\t";
        }
        result += "\n <P>Amount owed is <EM>" + getTotalAmount() + "</EM></P>";
        result += " \n On this rental you earned <EM>" + getTotalfrequentRenterPoints() + "</EM> frequent renter points<P>";
        return result;
    }

    public int getTotalfrequentRenterPoints() {
        int frequentRenterPoints = 0;
        Enumeration rental = _rentals.elements();
        String result = "Rental Record for " + getName();
        while (rental.hasMoreElements()) {
            Rental each = (Rental) rental.nextElement();
            frequentRenterPoints += each.getFrequentRetenterPoints();
            result += "\n\t   " + each.getMovie().getTitle() + "\t" + each.getAmount();
        }
        return frequentRenterPoints;
    }

    public double getTotalAmount() {
        double totalAmount = 0;
        Enumeration rental = _rentals.elements();
        while (rental.hasMoreElements()) {
            Rental each = (Rental) rental.nextElement();
            totalAmount += each.getAmount();
        }
        return totalAmount;
    }
}
